package access;

import static util.Print.*;

public class Exercise8 {

}

class ConnectionsManager {
  
  static final int QUANTITY = 5;
  
  static Connection[] connections = new Connection[QUANTITY];
  
  static {
    for (int i = 0; i < connections.length; i++) {
      connections[i] = new Connection();
    }
  }
 
  public static Connection getConnection() {
    for (int i = 0; i < connections.length; i++) {
      if (connections[i] != null) { 
        Connection con = connections[i];
        connections[i] = null;
        return con;
      }
    }
    return null;
  }
  
  public static void checkIn(Connection con) {
    for (int i = 0; i < connections.length; i++) {
      if (connections[i] == null) {
        connections[i] = con;
        return;
      }
    }
    print("Error: connections pool already full");
  }
    
}

class Connection {
  
  static int counter = 0;
  private int id = counter++;
  
  
  Connection() {}
  
  public void checkIn() {
    ConnectionsManager.checkIn(this);
  }
  
}